<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="title"
          title="通知标题"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.title" placeholder="请输入通知标题" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="300"
          field="noticeContent"
          title="通知内容"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.noticeContent" placeholder="请输入通知内容" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          width="240"
          field="gmtCreated"
          title="创建时间"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            class="unite-data"
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="推送入驻商" placement="top">
            <vxe-button type="text" @click="funcPushShipper(scope.row.code)">
              <i class="el-icon-share" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="推送司机" placement="top">
            <vxe-button type="text" @click="funcPushDriver(scope.row.code)">
              <i class="el-icon-position" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="500" resize>
      <compAdd @fatherComplete="fatherComplete" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="500" resize>
      <compEdit ref="editRef" @fatherComplete="fatherComplete" />
    </vxe-modal>
    <vxe-modal v-model="modelPushShipperFlag" title="推送入驻商选择" width="800" height="600" resize>
      <compPushShipper ref="pushShipperRef" @fatherComplete="fatherComplete" />
    </vxe-modal>
    <vxe-modal v-model="modelPushDriverFlag" title="推送司机选择" width="800" height="600" resize>
      <compPushDriver ref="pushDriverRef" @fatherComplete="fatherComplete" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, { mounts: ["cookie"] });
import compAdd from "./components/SMSManageAdd.vue";
import compEdit from "./components/SMSManageEdit.vue";
import compPushShipper from "./components/SMSManagePushShipper.vue";
import compPushDriver from "./components/SMSManagePushDriver.vue";
import { getNoteOrSMSManageList, delNoteOrSMSManage } from "@/api/news";
export default {
  name: "SMSManageList",
  components: {
    compAdd,
    compEdit,
    compPushShipper,
    compPushDriver
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      modelAddFlag: false,
      modelEditFlag: false,
      modelPushShipperFlag: false,
      modelPushDriverFlag: false,
      listQuery: {
        noticeType: 0,
        title: "",
        noticeContent: "",
        gmtCreatedStart: "",
        gmtCreatedEnd: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      }
    };
  },
  created() {
    this.findList();
  },
  methods: {
    // 列表
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getNoteOrSMSManageList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      });
    },
    // 日期
    changeDate(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0] + " 00:00:00";
        this.listQuery.gmtCreatedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 新增
    funcAdd() {
      this.modelAddFlag = true;
    },
    // 编辑
    funcEdit(code) {
      this.modelEditFlag = true;
      this.$refs.editRef.getInfo(code);
    },
    // 删除
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delNoteOrSMSManage({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 推送入驻商
    funcPushShipper(code) {
      this.modelPushShipperFlag = true;
      this.$refs.pushShipperRef.findList(code);
    },
    // 推送司机
    funcPushDriver(code) {
      this.modelPushDriverFlag = true;
      this.$refs.pushDriverRef.findList(code);
    },
    // 关闭弹框
    fatherComplete() {
      this.modelAddFlag = false;
      this.modelEditFlag = false;
      this.modelPushShipperFlag = false;
      this.modelPushDriverFlag = false;
      this.findList();
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
